<?php
namespace app\common\model;

class AnswerQuestion extends Base
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'a_time';
    protected $updateTime = 'u_time';
        
    /**
     * 统计用户今日答题数量
     * @param int $uid 用户ID
     * @param int $courseId 题库ID
     * @return int 今日答题数量
     */
    public function countTodayQuestionByUidAndCourseId($uid, $courseId)
    {
        // 获取今天的开始和结束时间戳
        $todayStart = strtotime(date('Y-m-d 00:00:00'));
        $todayEnd = strtotime(date('Y-m-d 23:59:59'));
        
        // 查询条件
        $where = [
            'user_id' => $uid,
            'course_id' => $courseId,
            ['a_time', '>=', $todayStart],
            ['a_time', '<=', $todayEnd]
        ];
        
        // 统计今日答题数量
        return $this->where($where)->count();
    }
    
    /**
     * 统计用户累计答题数量
     * @param int $uid 用户ID
     * @param int $courseId 题库ID
     * @return int 累计答题数量
     */
    public function countTotalQuestionByUidAndCourseId($uid, $courseId)
    {
        // 查询条件
        $where = [
            'user_id' => $uid,
            'course_id' => $courseId
        ];
        
        // 统计累计答题数量
        return $this->where($where)->count();
    }
    
    /**
     * 获取累计答题数量（支持更多条件筛选）
     * @param array $conditions 查询条件数组
     * @return int 累计答题数量
     */
    public function getAnswerCount($conditions = [])
    {
        // 构建查询对象
        $query = $this;
        
        // 应用查询条件
        if (!empty($conditions)) {
            $query = $query->where($conditions);
        }
        
        // 返回统计结果
        return $query->count();
    }
    
    /**
     * 统计用户累计答题正确率
     * @param int $uid 用户ID
     * @param int $courseId 题库ID
     * @return int 正确率(百分比整数)
     */
    public function calculateCorrectRateByUidAndCourseId($uid, $courseId)
    {
        // 查询条件
        $where = [
            'user_id' => $uid,
            'course_id' => $courseId
        ];
        
        // 总答题数
        $total = $this->where($where)->count();
        
        if ($total <= 0) {
            return 0;
        }
        
        // 正确答题数
        $correct = $this->where($where)->where('status', 1)->count();
        
        // 计算正确率(四舍五入为整数)
        return round(($correct / $total) * 100);
    }
    
    /**
     * 获取用户答题统计数据
     * @param int $uid 用户ID
     * @param int $courseId 题库ID
     * @return array 包含今日答题数、累计答题数和正确率的数组
     */
    public function getUserAnswerStatsByUidAndCourseId($uid, $courseId)
    {
        return [
            'today_question_count' => $this->countTodayQuestionByUidAndCourseId($uid, $courseId),
            'total_question_count' => $this->countTotalQuestionByUidAndCourseId($uid, $courseId),
            'correct_rate' => $this->calculateCorrectRateByUidAndCourseId($uid, $courseId)
        ];
    }
    
    /**
     * 获取试卷答题统计数据
     * @param array $paperIds 试卷ID数组(extend_id)
     * @param int $uid 用户ID，可选，默认为0表示所有用户
     * @return array 包含累计答题数和正确率的数组
     */
    public function getPaperAnswerStats($paperIds, $uid = 0)
    {
        // 查询条件
        $where = [
            ['extend_id', 'in', $paperIds],
            'user_id' => $uid
        ];
        
        // 总答题数
        $totalCount = $this->where($where)->count();

        // 正确答题数
        $correctCount = $this->where($where)->where('status', 1)->count();
        
        // 正确率
        $correctRate = $totalCount > 0 ? round(($correctCount / $totalCount) * 100) : 0;
        
        return [
            'total_question_count' => $totalCount,
            'total_correct_count' => $correctCount,
            'correct_rate' => $correctRate
        ];
    }
    
}